# -*- coding: utf-8 -*-

from odoo import models, fields, api


# class shilimokuai002(models.Model):
#     _name = 'shilimokuai002.shilimokuai002'
#     _description = 'shilimokuai002.shilimokuai002'

#     name = fields.Char()
#     value = fields.Integer()
#     value2 = fields.Float(compute="_value_pc", store=True)
#     description = fields.Text()
#
#     @api.depends('value')
#     def _value_pc(self):
#         for record in self:
#             record.value2 = float(record.value) / 100
class WebsiteCategory(models.Model):
    _name = 'website.category'
    _description = 'Website Category'
    _parent_store = True
    _rec_name = 'complete_name' 

    name = fields.Char(string='Category Name', required=True)
    parent_id = fields.Many2one(
        'website.category',
        string='Parent Category',
        index=True,
        ondelete='cascade'
    )
    child_ids = fields.One2many(
        'website.category',
        'parent_id',
        string='Child Categories'
    )

    parent_path = fields.Char(index=True)
    complete_name = fields.Char(string="完整路径", compute="_compute_complete_name", store=True)


    @api.depends('name', 'parent_id.complete_name')
    def _compute_complete_name(self):
        for rec in self:
            if rec.parent_id:
                rec.complete_name = f"{rec.parent_id.complete_name} / {rec.name}"
            else:
                rec.complete_name = rec.name

    def name_get(self):
        return [(rec.id, rec.complete_name) for rec in self]
 

class RegisterCertCategory(models.Model):
    _name = 'register.cert.category'
    _description = 'Register Certificate Category'
    _parent_store = True  # 启用 tree 结构索引优化
    
    _rec_name = 'complete_name' 
    name = fields.Char(string='Category Name', required=True)
    parent_id = fields.Many2one(
        'register.cert.category',
        string='Parent Category',
        index=True,
        ondelete='cascade'
    )
    child_ids = fields.One2many(
        'register.cert.category',
        'parent_id',
        string='Child Categories'
    )
 
    parent_path = fields.Char(index=True)  # 自动用于递归层级计算
    complete_name = fields.Char(string="完整路径", compute="_compute_complete_name", store=True)

    @api.depends('name', 'parent_id.complete_name')
    def _compute_complete_name(self):
        for rec in self:
            if rec.parent_id:
                rec.complete_name = f"{rec.parent_id.complete_name} / {rec.name}"
            else:
                rec.complete_name = rec.name

    def name_get(self):
        return [(rec.id, rec.complete_name) for rec in self]

class pinpai(models.Model):
    _name = 'shilimokuai002.pinpai'
    _description = 'shilimokuai002.pinpai'

    name = fields.Char()
    guochanjinkou = fields.Selection([('guochan', '国产'), ('jinkou', '进口')], string='是否进口')

class chanpin(models.Model):
    _name = 'shilimokuai002.chanpin'
    _description = 'shilimokuai002.chanpin'

    name = fields.Char(string='产品名称')
    chanpinbieming = fields.Char(string='产品别名')
    chanpinxinghao = fields.Char(string='产品型号')
    kexiaoshou=fields.Boolean(string='可销售')
    kecaigou=fields.Boolean(string='可采购')
    keshangjia=fields.Boolean(string='可上架')
    description = fields.Text()
    pingji = fields.Selection([('ss', 'ss'), ('sss', 'sss')], string='产品评级')
    pinpai_id = fields.Many2one('shilimokuai002.pinpai', string='品牌')
    guochan = fields.Selection(related='pinpai_id.guochanjinkou', string='是否国产')
    category_id = fields.Many2one('register.cert.category', string='注册证分类')
    webcategory_id = fields.Many2one('website.category', string='企晟网站目录1')
    chengbenjia = fields.Float(string='成本价')
    xiaoshoujia = fields.Float(string='销售价格')
    shichangjia = fields.Float(string='市场价')
    xiaoshoushuixiang=fields.Float(string='销售税项')
    shifouyibao=fields.Boolean(string='是否医保')
    shifoupeitao=fields.Boolean(string='是否配套')
    zuixiaogoumai = fields.Float(string='最小购买数量')
    goumaidanwei = fields.Char(string='购买单位')
    xianggui = fields.Char(string='箱规')
    huoqi = fields.Float(string='货期')
    beizhu=fields.Text(string='备注')   
    image_1920 = fields.Image(string="产品图片")

    @api.depends('value')
    def _value_pc(self):
        for record in self:
            record.value2 = float(record.value) / 100